#include "configfile.h"
#include <QTextCodec>
#include <QCoreApplication>
#include <QDebug>


ConfigFile::ConfigFile(QObject *parent) : QObject(parent)
  , m_settings(NULL)
{
    m_docParhFile =QCoreApplication::applicationDirPath();
    m_docParhFile+="/config.ini";
    m_settings= new QSettings (m_docParhFile, QSettings ::IniFormat);
    m_settings->setIniCodec(QTextCodec::codecForName("UTF-8"));

    qDebug()<<"ConfigFile::ConfigFile"<<__func__<<__LINE__<<"m_docParhFile =="<<m_docParhFile;
    //initAllData();
}

ConfigFile::~ConfigFile()
{

}

ConfigFile *ConfigFile::gestance()
{
    static ConfigFile  configFile;

    return  &configFile;
}

void ConfigFile::initAllData()
{
    setSinW(QString("360"));

    setSinH(QString("10"));

    setAirTime(QString("180"));

    setCoTime(QString("180"));


    setAirCheck(QString("0.01"));

    setCoCheck(QString("0.01"));



    setAirSpace(QString("5"));

    setCoSpace(QString("5"));


    setAirTakeTime(QString("20"));

    setCoTakeTime(QString("20"));

}

void ConfigFile::setSinW(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("sinw",values);
        m_settings->endGroup();
    }
}

int ConfigFile::getSinW()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("sinw",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="360";
    }
    return stra.toInt();
}

void ConfigFile::setSinH(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("sinh",values);
        m_settings->endGroup();
    }
}

int ConfigFile::getSinH()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("sinh",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="10";
    }
    return stra.toInt();
}

void ConfigFile::setAirTime(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("airTime",values);
        m_settings->endGroup();
    }
}

int ConfigFile::getAirTime()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("airTime",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="180";
    }
    return stra.toInt();
}

void ConfigFile::setCoTime(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("coTime",values);
        m_settings->endGroup();
    }
}

int ConfigFile::getCoTime()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("coTime",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="180";
    }
    return stra.toInt();
}

void ConfigFile::setAirCheck(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("airCheck",values);
        m_settings->endGroup();
    }
}

double ConfigFile::getAirCheck()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("airCheck",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="0.01";
    }
    return stra.toDouble();
}

void ConfigFile::setAirTake(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("airTake",values);
        m_settings->endGroup();
    }
}

int ConfigFile::getAirTake()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("airTake",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="180";
    }
    return stra.toInt();
}

void ConfigFile::setCoCheck(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("coCheck",values);
        m_settings->endGroup();
    }
}

double ConfigFile::getCoCheck()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("coCheck",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="0.01";
    }
    return stra.toDouble();
}

void ConfigFile::setAirSpace(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("airSpace",values);
        m_settings->endGroup();
    }
}

int ConfigFile::getAirSpace()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("airSpace",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="5";
    }
    return stra.toInt();

}

void ConfigFile::setCoSpace(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("coSpace",values);
        m_settings->endGroup();
    }
}

int ConfigFile::getCoSpace()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("coSpace",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="5";
    }
    return stra.toInt();
}

void ConfigFile::setAirTakeTime(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("airTakeTime",values);
        m_settings->endGroup();
    }
}

int ConfigFile::getAirTakeTime()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("airTakeTime",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="20";
    }
    return stra.toInt();
}

void ConfigFile::setCoTakeTime(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("coTakeTime",values);
        m_settings->endGroup();
    }
}

int ConfigFile::getCoTakeTime()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("coTakeTime",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="20";
    }
    return stra.toInt();
}

void ConfigFile::setCoTake(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("coTake",values);
        m_settings->endGroup();
    }
}

int ConfigFile::getCoTake()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("coTake",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="20";
    }
    return stra.toInt();
}

void ConfigFile::setOpenTime(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("openTime",values);
        m_settings->endGroup();
    }
}

int ConfigFile::getOpenTime()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("openTime",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="0";
    }
    return stra.toInt();
}

void ConfigFile::setRunNum(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("runNum",values);
        m_settings->endGroup();
    }
}

int ConfigFile::getRunNum()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("runNum",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="1";
    }
    return stra.toInt();
}

void ConfigFile::setPowers(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("powers",values);
        m_settings->endGroup();
    }
}

int ConfigFile::getPowers()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("powers",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="0";
    }
    return stra.toInt();
}

void ConfigFile::setLimitNum(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("limitNums",values);
        m_settings->endGroup();
    }
}

double ConfigFile::getLimitNum()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("limitNums",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="3.5";
    }
    return stra.toDouble();
}

void ConfigFile::setLimitTime(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("limitTimes",values);
        m_settings->endGroup();
    }
}

int ConfigFile::getLimitTime()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("limitTimes",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="180";
    }
    return stra.toInt();
}

void ConfigFile::setLimitDevice(QString values)
{
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        m_settings->setValue("limitDevices",values);
        m_settings->endGroup();
    }
}

int ConfigFile::getLimitDevice()
{
    QString stra = "";
    if(m_settings != NULL){
        m_settings->beginGroup("config");
        stra = m_settings->value("limitDevices",QVariant("")).toString();

        m_settings->endGroup();
    }
    if(stra.length() <= 0){
        stra="0";
    }
    return stra.toInt();
}
